package com.credithc.signature.manager.cron;

import com.credithc.baseserv.core.utils.HttpUtils;
import com.credithc.signature.common.mapper.SealInfoMapper;
import com.credithc.signature.common.po.SealInfo;
import com.credithc.signature.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @desc: 证书到期提醒定时任务<br />
 * @author: Jingbo Hu<br/>
 * @time：2020年05⽉12⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
@Component
@Async
public class CertAlarmCron {

    @Value("${signature.qywxsend.url:http://10.10.255.196/api/v2/qywxsend/text}")
    private String sendUrl;

    @Value("${signature.qywxsend.alarm.members:wangdapeng180606@credithc.com}")
    private String members;

    @Autowired
    SealInfoMapper sealInfoMapper;


    /**
     * 每天早上8点执行一次
     */
    @Scheduled(cron = "0 0 8 * * ?")
    public void alarmCron() {
        log.info("执行定时任务[查询过期公章信息]每天执行一次,现在时间为：" + DateUtil.getDefaultDateTimeStr());
        List<SealInfo> sealInfos = sealInfoMapper.listSealInfos();
        for (SealInfo sealInfo : sealInfos) {
            // 计算过期时间
            Date createTime = sealInfo.getCreateTime();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(createTime.toInstant(), ZoneId.systemDefault());
            LocalDateTime localDateTime1 = localDateTime.plusYears(2);
            // 当前时间的前一个月
            LocalDateTime localDateTime2 = DateUtil.getLocalDateTime(new Date()).minusMonths(1);
            // 当前时间的前两个周
            LocalDateTime localDateTime3 = DateUtil.getLocalDateTime(new Date()).minusWeeks(2);

            // 如果localDateTime1 小于 localDateTime2 说明证书过期了，需要发送企业微信信息。证书默认有效期是2年
            boolean after = localDateTime1.isAfter(localDateTime2);
            if (!after) {
                String format = localDateTime1.format(DateTimeFormatter.ofPattern(DateUtil.DEFAULT_PATTERN));
                log.info(sealInfo.getSealName() + "公章证书将在[" + format + "]过期");
                String expireDate = DateUtil.getDateStr(localDateTime1);
                String sendMsg = "【共享中台】" + sealInfo.getSealName() + "公章证书即将到期,\n" + sealInfo.getSealName() + "公章证书将于" + expireDate + "到期，请及时通知企业相关人员续费，公司电话：" + sealInfo.getContactPhone();
                String[] split = members.split(",");
                for (String s : split) {
                    HashMap map = new HashMap(16);
                    map.put("key", "19c487e205cbdfda0690778e27825b6d2669c031");
                    map.put("to", s);
                    map.put("body", sendMsg);
                    String res = HttpUtils.sendPostMsg(sendUrl, map);
                    log.info("发送企业微信消息成功......{}", res);
                }
            }
        }
    }

}
